<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/14
 * Time: 19:13
 */

namespace app\storage\controller;

use app\storage\model\ContainerModel;
use app\storage\service\MaterielService;
use app\storage\service\TagService;
use app\storage\model\SupplierModel;
use app\storage\model\PurchaseDetailModel;
use cmf\controller\AdminBaseController;
use think\Request;

class TagController extends AdminBaseController
{

    private $purchaseDetailModel;
    private $containerModel;

    public function __construct(PurchaseDetailModel $purchaseDetailModel,ContainerModel $containerModel, $request = null)
    {
        parent::__construct($request);
        $this->purchaseDetailModel = $purchaseDetailModel;
        $this->containerModel = $containerModel;
    }

    public function materiel()
    {
        $supplierModel = new SupplierModel();
        $supplierInfo = $supplierModel->getCompany();
        $this->assign('supplierInfo', $supplierInfo);
        return $this->fetch('materiel');
    }

    public function copy()
    {
        return $this->fetch('copy');
    }

    public function tag()
    {
        return $this->fetch('tag');
    }

    /**
     * 根据物料号获取物料信息并返回新生成的reelId
     */
    public function getMaterielInfo()
    {
        $materielId = input('materiel');
        $materielInfo = MaterielService::create_materiel($materielId);
        if($materielInfo['success'] == false){
            $ret = ret_template(false,'不存在料号'.$materielId.'信息');
        }else{
            $ret = TagService::createReelIdByMateriel($materielId);
        }
        return json($ret);
    }


    public function getReelInfo()
    {
        $isCopy = input('copy');
        $reelId = input('reel');
        $reelInfo = $this->purchaseDetailModel->where('reel_id',$reelId)->order('reel_id desc')->find();
        if(!count($reelInfo)){
            $ret = ret_template(false,'不存在条码'.$reelId.'信息');
        }else{
            $isCopy = $isCopy == 'true'? true:false;
            $ret = TagService::createReelIdByReelId($reelId,$isCopy);
        }
        return json($ret);
    }

    public function insertReelToPurchase()
    {
        $this->purchaseDetailModel->startTrans();
        $reelId = input('reel');
        $materiel = input('materiel');
        $count = intval(input('count'));
        $supplier = input('supplier');
        $addContainer = input('addContainer');

        if($addContainer == 'false'){//checkbox传入的值为字符串，要先转换成布尔值，否则没办法比较，因为字符串恒为真
            $addContainer = false;
        }
        else{
            $addContainer = true;
        }

        $excision = substr($reelId, 12, 1);
        $reelInfo = $this->purchaseDetailModel->where('reel_id',$reelId)->lock(true)->find();
        if($excision != '-' && count($reelInfo)){
            return ret_template(false,'条码信息已过期请返回页面重新生成');
        }
        $reelInfo = [
            'uuid'=>create_uuid(),
            'purchase_id'=>create_unique_id('manual'),
            'reel_id'=>$reelId,
            'count'=>$count,
            'sub_materiel_id'=>$materiel,
            'supplier_id'=>$supplier,
            'remark'=>'copy',
            'time'=>get_current_time()
        ];
        $result = $this->purchaseDetailModel->insert($reelInfo,true);
        $this->purchaseDetailModel->commit();
        $msg = $result?'插入数据库成功':'插入数据库失败';
        $this->containerModel->startTrans();
        if ($addContainer == true){//为true说明要根据物料号生成容器。
            $this->containerModel->addContainer($reelId);
        }
        $this->containerModel->commit();

        return ret_template(boolval($result),$msg);
    }
}